package com.zzq.controller;

import com.zzq.pojo.User;
import com.zzq.service.user.UserService;
import com.zzq.util.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;
    @RequestMapping("/User/login")
    public String login(Model model, HttpServletRequest request, HttpServletResponse response){
        System.out.println("LoginServlet--start....");
        //获取用户名密码
        String userCode = request.getParameter("userCode");
        String userPassword = request.getParameter("userPassword");
        User user = userService.login(userCode, userPassword);//这里已经把登陆的任查出来了
        if(user!=null){
            request.getSession();
            request.getSession().setAttribute(Constants.USER_SESSION,user);
            request.getSession().setAttribute(Constants.USERNAME_SESSION,user.getUserName());

        }else{
            request.setAttribute("error","用户名或者密码不正确");
            try {
                request.getRequestDispatcher("/login").forward(request,response);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Constants.USER_SESSION);
        return "main";
    }

    @RequestMapping("/User/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        request.getSession().removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    @RequestMapping("/User/register")
    public String register(HttpServletRequest request,HttpServletResponse response){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
        String isit = request.getParameter("name");
        String pwd = request.getParameter("pwd1");
        User user = userServiceImpl.getLoginUserByName(isit);
        if (user==null){
            userServiceImpl.register(isit,pwd);
            return "login";
        }else{
            request.setAttribute("repeat","该用户名已被注册!");
            return "register";
        }
    }

    @RequestMapping("/User/changePwd/{userName}")
    public String changePwd(@PathVariable("userName")String userName, HttpServletRequest request, HttpServletResponse response){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
        String pwd = request.getParameter("pwd1");
        userServiceImpl.changePassword(userName,pwd);
        return "main";
    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest req){
        req.getSession().removeAttribute(Constants.USER_SESSION);
        return "login";
    }
}
